package com.xzy.common.util;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import org.apache.logging.log4j.util.Strings;

/**
 * AES 加密/解密工具
 *
 * @author xzy.xiao
 * @date 2022/8/4  20:54
 */
public class AesUtil {

    private static final String KEY_STR = "ad1725339b2dd0a68903c57b635942ca";
    private static final byte[] KEY_BYTES;

    static {
        KEY_BYTES = new byte[16];
        int i = 0;
        for (byte b : KEY_STR.getBytes()) {
            KEY_BYTES[i++ % 16] ^= b;
        }
    }

    public static String encrypt(String content) {
        if (Strings.isBlank(content)) {
            return content;
        }
        return HexUtil.encodeHexStr(SecureUtil.aes(KEY_BYTES).encrypt(content), false);
    }

    public static String decrypt(String content) {
        if (Strings.isBlank(content)) {
            return content;
        }
        return SecureUtil.aes(KEY_BYTES).decryptStr(content);
    }

    public static void main(String[] args) {
        String encrypted = encrypt("测试");
        System.out.println(encrypted);
        System.out.println(decrypt(encrypted));
    }
}
